/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.httpservice;

import cn.ac.iie.di.wx.utils.DateUtil;
import static cn.ac.iie.di.wx.utils.DateUtil.transformDateFormate;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class HttpGetter {

    static Logger logger = Logger.getLogger(HttpGetter.class.getName());

    public void doGet(String request) {
        try {
            String response = org.apache.http.client.fluent.Request.Get(request)
                    .execute().returnContent().asString();
            JSONObject jSONObject = JSONObject.parseObject(response);
            String isSuccess = jSONObject.getString("success");
            if (isSuccess.equals("false")) {
                Thread.sleep(1000 * 60 * 30);
                String response2 = org.apache.http.client.fluent.Request.Get(request)
                        .execute().returnContent().asString();
                logger.info(DateUtil.getToDayDate() + "+" + jSONObject.getString("message"));
                JSONObject jSONObject2 = JSONObject.parseObject(response2);
                String isSuccess2 = jSONObject2.getString("success");
                if (isSuccess2.equals("false")) {
                    //再次错误就发送错误信息反馈发送的方式，对象，以及错误信息的编写
                    logger.info(DateUtil.getToDayDate() + "+" + jSONObject2.getString("message"));
                }
            } else {
                logger.info(DateUtil.getToDayDate() + "+" + jSONObject.getString("message"));
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } catch (InterruptedException ex) {
            java.util.logging.Logger.getLogger(HttpGetter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void doGetByDate(String request,String date){
        try {
            String response = org.apache.http.client.fluent.Request.Get(request)
                    .execute().returnContent().asString();
            JSONObject jSONObject = JSONObject.parseObject(response);
            String isSuccess = jSONObject.getString("success");
            if (isSuccess.equals("false")) {
                Thread.sleep(1000 * 60 * 30);
                String response2 = org.apache.http.client.fluent.Request.Get(request)
                        .execute().returnContent().asString();
                logger.info(transformDateFormate(date) + "+" + jSONObject.getString("message"));
                JSONObject jSONObject2 = JSONObject.parseObject(response2);
                String isSuccess2 = jSONObject2.getString("success");
                if (isSuccess2.equals("false")) {
                    //再次错误就发送错误信息反馈发送的方式，对象，以及错误信息的编写
                    logger.warn(transformDateFormate(date) + "+" + jSONObject2.getString("message"));
                }
            } else {
                logger.info(transformDateFormate(date) + "+" + jSONObject.getString("message"));
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } catch (InterruptedException ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}
